<?php
/**
 * User: hjd-yao
 * Date: 2019/4/18
 * Time: 10:05
 */

namespace App\Http\Controllers\Shop;


use App\Helpers\SelfResponse;
use App\Http\Controllers\BackendController;
use App\Models\Shop\ShopRuleModel;
use Illuminate\Http\Request;

class ShopRuleController extends BackendController
{
    /**
     * @title 商城规则
     * @return View
     */
    public function rules()
    {
        $rule = ShopRuleModel::findOrNew(1);
        if ($rule->th_open_time){
            $rule->th_open_time = str2arr($rule->th_open_time);
        }else{
            $rule->th_open_time = ['00:00:00 - 12:00:00','12:00:01 - 23:59:59'];
        }

        return view('backend::shop.rule',[
            'page_title'=>'商城规则',
            'rule'=>$rule
        ]);
    }

    /**
     * @title 更新商城规则
     * @param Request $request
     * @return SelfResponse|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
     */
    public function update(Request $request)
    {
        $data = $request->except('_token');
        $data['th_open_time'] = arr2str($data['th_open_time']);
        if(!ShopRuleModel::updateOrCreate(['id'=>1],['id'=>1]+$data)){
            return SelfResponse::error('规则更新失败!');
        }
        return SelfResponse::success('规则更新成功');
    }

}